<?php

namespace App\Models\Customer;

use App\Models\Panx\Product;
use Illuminate\Database\Eloquent\Model;

/**
 * Class CustomerRelateWaxSkin
 *
 * @mixin \Illuminate\Database\Eloquent\Builder
 * @mixin \Illuminate\Database\Query\Builder
 *
 * @property string id
 * @property int item_id
 * @package App\Models\Customer
 */
class CustomerRelateWaxSkin extends Model
{
    protected $table = 'customer_relate_wax_skin';
    protected $guarded = [];

    const CUSTOMER_ID_DEFAULT = 0; // 默认customer_id, 关联默认的wax护肤建议。
    const STORE_ID_DEFAULT = 0; // 默认store_id, 关联默认的wax护肤建议。

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function waxSuggestByLanguage()
    {
        $language_id = Language::getLanguageIds()[app()->getLocale()];

        return $this->hasOne(WaxSkinSuggest::class, 'customer_relate_wax_skin_id', 'id')
            ->where('language_id', $language_id);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
     */
    public function products()
    {
        return $this->morphToMany(Product::class, 'productable', 'customer_relate_skin_productables');
    }

    /**
     * @param $customer_id
     * @return int
     */
    public static function hasWaxSkinSuggest($customer_id)
    {
        $customer_has_suggest = self::checkWaxSkinSuggest($customer_id);

        return $customer_has_suggest ? $customer_id : self::CUSTOMER_ID_DEFAULT;
    }

    /**
     * @param $customer_id
     * @return bool
     */
    public static function checkWaxSkinSuggest($customer_id)
    {
        $customer_has_suggest = (new self)
            ->where(compact('customer_id'))
            ->where('store_id', self::STORE_ID_DEFAULT)
            ->has('waxSuggestByLanguage')
            ->exists();

        return $customer_has_suggest;
    }

    /**
     * @param $item_id
     * @param $level_sort
     * @param $customer_id
     * @param $store_id
     * @return CustomerRelateWaxSkin[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|\Illuminate\Support\Collection
     */
    public static function getCustomerRelateWaxSkin($item_id, $level_sort, $customer_id, $store_id)
    {
        return (new self)->where('customer_id', $customer_id)
            ->whereIn('store_id', [$store_id, CustomerRelateWaxSkin::STORE_ID_DEFAULT])
            ->where('item_id', $item_id)
            ->where('level_sort', $level_sort)
            ->latest('store_id')
            ->get();
    }

    /**
     * @param $customer_id
     * @param $store_id
     * @return bool
     */
    public static function hasCustomerRelateWaxSkin($customer_id, $store_id)
    {
        return (new self)->where(compact('customer_id'))
            ->where(compact('store_id'))
            ->exists();
    }

    /**
     * @param $customer_id
     * @param $store_id
     * @param $skin_ids
     * @return CustomerRelateWaxSkin[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getCustomerRelateWaxSkinByIds($customer_id, $store_id, $skin_ids)
    {
        return (new self)->where(compact('customer_id'))
            ->where(compact('store_id'))
            ->whereIn('id', $skin_ids)
            ->get();
    }

    /**
     * @return CustomerRelateWaxSkin[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getPanxCustomerRelateWaxSkins()
    {
        return (new self)->where('customer_id', self::CUSTOMER_ID_DEFAULT)
            ->where('store_id', self::STORE_ID_DEFAULT)
            ->with('waxSuggestByLanguage')
            ->get();
    }
}
